package com.huluobo.a2107mvpimdemo.base;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.huluobo.a2107mvpimdemo.utils.NetUtils;

/**
 * Activity基准类
 * Created by LC on 2023/9/20.
 */
public abstract class BaseActivity<V, P extends BasePresenter<V>> extends AppCompatActivity {
    protected P presenter;

    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(bindLayoutId());
        initView();
        initData();
        if (presenter != null) {
            presenter.attachView((V) this);
        }
    }

    protected abstract void initData();

    protected abstract void initView();

    protected abstract int bindLayoutId();

    public void showProgressDialog(String message) {
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage(message);
        progressDialog.show();
    }

    public void dismissProgressDialog() {
        if (progressDialog.isShowing() && progressDialog != null) {
            progressDialog.dismiss();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!NetUtils.getNetUtils(this).isConnected()) {
            Toast.makeText(this, "当前没有网络连接,请检查网络状态", Toast.LENGTH_SHORT).show();
        }

        if (NetUtils.getNetUtils(this).isSlowlyNetWork()) {
            Toast.makeText(this, "当前网络有些慢,请检查网络连接", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.disAttach();
        }
    }
}
